package com.heatup.apt.api.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.heatup.apt.api.service.ProductServiceService;
import com.heatup.apt.api.service.UserProductServiceService;
import com.heatup.apt.common.constants.UserProductServiceConstants;
import com.heatup.apt.common.dao.OperativeMapper;
import com.heatup.apt.common.dao.ProdeuctServiceApiModeMapper;
import com.heatup.apt.common.dao.ProductServiceDetailMapper;
import com.heatup.apt.common.dao.ProductServiceMapper;
import com.heatup.apt.common.dao.store.StoreInfoMapper;
import com.heatup.apt.common.dao.user.UserProductServiceMapper;
import com.heatup.apt.common.util.DateUtils;
import com.heatup.apt.model.api.store.StoreResultModel;
import com.heatup.apt.model.management.VipActivity;
import com.heatup.apt.model.management.VipFigure;
import com.heatup.apt.model.product.ProdeuctServiceApiMode;
import com.heatup.apt.model.product.ProductService;
import com.heatup.apt.model.product.ProductServiceDetail;
import com.heatup.apt.model.store.StoreInfo;
import com.heatup.apt.model.user.UserProductService;



/**
 *
 * ProductService 表数据服务层接口实现类
 *
 */
@Service
public class ProductServiceServiceImpl extends SuperServiceImpl<ProductServiceMapper, ProductService> implements ProductServiceService {
	
	Logger logger = LoggerFactory.getLogger(ProductServiceServiceImpl.class); 
	
	@Autowired
	private ProductServiceMapper productServiceMapper;
	
	@Autowired
	private StoreInfoMapper storeInfoMapper; 
	
	@Autowired
	private UserProductServiceMapper  userProductServiceMapper;
	
	@Autowired
	private ProdeuctServiceApiModeMapper  prodeuctServiceApiModeMapper;
	
	@Autowired
	public ProductServiceDetailMapper productServiceDetailMapper;
	
	@Autowired
	private OperativeMapper operativeMapper;
	
	@Autowired
	private UserProductServiceService userProductServiceService;
	
	/**
	 * 获取产品列表
	 */
	@Override
	public Map<String, Object> queryProductServiceList(Map<String, Object> parameMap) {
		logger.info("=============================queryProductServiceList start========================");
		String type = (String) parameMap.get("type");
		Integer userId = (Integer) parameMap.get("userId");
		List<ProductService> list = null;
		if("1".equals(type)){
			// 查询所有的
			list = productServiceMapper.queryProductServiceListAll();
			parameMap.put("list", list);// 所有的产品
		}else{
			// 已开通的产品
			List<ProdeuctServiceApiMode> openList = prodeuctServiceApiModeMapper.queryProdeuctServiceOpenList(parameMap);
			// 未开通的产品
			List<String> openTypeList = userProductServiceMapper.queryUserProductServiceType(userId);
			parameMap.put("typeList", openTypeList);
			if(openTypeList.size() == 0){
				parameMap.clear();
			}
			list = productServiceMapper.queryProductServiceList(parameMap);
			// 获取活动banner图
			List<VipFigure> listBanner = operativeMapper.queryVipFigureList();
			VipActivity vipActivity = operativeMapper.queryVipActivityOne();
			
			
			parameMap.clear();
			parameMap.put("openList", openList);
			parameMap.put("list", list);
			parameMap.put("vipBannerList", listBanner);
			parameMap.put("vipActivity", vipActivity);
		}
		
		logger.info("=============================queryProductServiceList end========================");
		return parameMap;
	}
	
	/**
	 * 根据产品id获取产品明细列表
	 */
	@Override
	public Map<String, Object> queryProductServiceStore(Map<String, Object> parameMap) {
		logger.info("=============================queryProductServiceStore start========================");
		
		Integer id = (Integer) parameMap.get("id");
		Integer userId = (Integer) parameMap.get("userId");
		// 查询所有的
		List<ProductService> listPs = productServiceMapper.queryProductServiceListAll();
		List<Map<String,Object>> listAll = new ArrayList<>();
		String longitudeLatitude = (String) parameMap.get("longitudeLatitude");
		if(StringUtils.isNotBlank(longitudeLatitude) && longitudeLatitude.split(",").length == 2){
			parameMap.put("latitude", longitudeLatitude.split(",")[0]);
			parameMap.put("longitude", longitudeLatitude.split(",")[1]);
        }
		parameMap.put("status", 2);
		for(ProductService ps : listPs){
			if(ps != null){
				// 在查询可支持的门店
				Map<String,Object> map = new HashMap<String, Object>();
				String [] str = ps.getPStoreListLimit().split("#");
				List<String> listStoreId = java.util.Arrays.asList(str);
				parameMap.put("list", listStoreId);
				parameMap.remove("userId");
				if(ps.getId() == 1 || ps.getPIsStoreListLimit().intValue() == 1){
					parameMap.remove("list");
				}
				List isSelfFintnessList = new ArrayList<String>();
				//仅查询支持自助的门店
				isSelfFintnessList.add("1");
				isSelfFintnessList.add("0");
				isSelfFintnessList.add("2");
				parameMap.put("isSelfFintnessList",isSelfFintnessList);
				List<StoreResultModel> storeList = storeInfoMapper.queryAllStore(parameMap);
				UserProductService us = userProductServiceMapper.queryUserProductServiceByUserIdAndType(userId, ps.getPServiceType());
				int isvalid = 0 ; // 0 未开通 1 已开通 2 待生效
				String vipEffectiveTime = "";
				Integer surplusDayCount = 0;
				Integer convertDayCount = 0;
				String cardNo = "";
				
				if(us != null){
					isvalid = userProductServiceService.checkUserHasYearCardService(userId);
					// 使用中
					if(isvalid == 1) {
						vipEffectiveTime = DateUtils.toDateString(us.getBeginDate()).replaceAll("-", ".")+
								"~"+DateUtils.toDateString(DateUtils.AddDays(us.getEndDate(), -1)).replaceAll("-", ".");
						surplusDayCount = userProductServiceService.getUserHasYearCardSurplusDateCount(us);
						convertDayCount = userProductServiceService.getUserHasYearCardConvertDateCount(userId);
					}else if(isvalid == 2) {
						// 待开通
						vipEffectiveTime = DateUtils.toDateString(us.getBeginDate())+"~"+DateUtils.toDateString(DateUtils.AddDays(us.getEndDate(), -1));
						surplusDayCount = userProductServiceService.getUserHasYearCardSurplusDateCount(us);
						convertDayCount = userProductServiceService.getUserHasYearCardConvertDateCount(userId);
					}
					cardNo = "WQHJ"+String.format("%08d",us.getId());
				}
				parameMap.put("storeList", storeList);
				map.put("productService", ps);
				map.put("storeList", storeList);
				map.put("isvalid",isvalid);//1:有效 0:无效
				map.put("vipEffectiveTime", vipEffectiveTime);
				
				map.put("surplusDayCount", surplusDayCount);
				map.put("convertDayCount", convertDayCount);
				map.put("cardNo", cardNo);
				
				listAll.add(map);
			}
			
		}
		parameMap.clear();
		parameMap.put("all", listAll);
		logger.info("=============================queryProductServiceStore end========================");
		return parameMap;
	}
	
	/**
	 * 根据服务id查询服务详情列表
	 */
	@Override
	public Map<String, Object> queryProductServiceDetail(Map<String, Object> parameMap) {
		logger.info("=============================queryProductServiceDetail start========================");
		Integer id = (Integer) parameMap.get("id");
		Integer userId = (Integer) parameMap.get("userId");
		// 先查询产品
		ProductService productService = productServiceMapper.selectById(id);
		// 查询常去门店
		List<StoreInfo> storeList = storeInfoMapper.queryAllStoreList();
		// 查询出我所拥有的产品
		List<String> listUserProduct = userProductServiceMapper.queryUserProductServiceType(userId);
		
		if(productService != null){
			// 查询产品明细
			List<ProductServiceDetail> listD = productServiceDetailMapper.queryProductServiceDetailListByProduceId(id);
			parameMap.put("productDetailList", listD);
		}
		logger.info("=============================queryProductServiceDetail end========================");
		return parameMap;
	}
	
	
	public Map<String,Object> getUserYearCardDetail(Integer userId){
		
		return null;
	}


}